import cv2

def find_and_highlight_target(a_image, target_image):
    # 读取图片a.png
    a_img = cv2.imread(a_image)
    # 读取目标图片3.png
    target_img = cv2.imread(target_image)

    # 在a.png中搜索目标图片3.png
    result = cv2.matchTemplate(a_img, target_img, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

    # 如果匹配度超过阈值，则在a.png中框出目标区域
    if max_val >= 0.4:
        target_height, target_width, _ = target_img.shape
        top_left = max_loc
        bottom_right = (top_left[0] + target_width, top_left[1] + target_height)
        cv2.rectangle(a_img, top_left, bottom_right, (0, 255, 0), 2)

        # 显示框出目标后的图片a.png
        cv2.imshow('Highlighted Image', a_img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    else:
        print("Target not found in the image")

# 调用函数检测并在a.png中框出目标3.png
find_and_highlight_target("icon/img.png", "icon/3.png")